<?php
include_once('./config/config.php');
$admin = AuthCheck();
$action = $_GET['action'] ?? '';
// 退出账号
if ($action === 'logout') {
    setcookie('auth', '', time() - 1);
    notice('退出成功', 'login.php');
}


// 当前页
$page = $_GET['page'] ?? 1;
$CountJob = "SELECT COUNT(*) AS `c` FROM `pre_job`";
$count = find($CountJob)['c'];  // 13
$limit = 10; // 限制查询和显示
$start = ($page - 1) * $limit; // 从第几条数据开始查找
$JobSql = "SELECT `j` . *, `d` . `name` AS `dname` FROM `pre_job` AS `j` LEFT JOIN `pre_department` AS `d` ON `j` . `depid` = `d` . `id` ORDER BY `j` . `id` LIMIT $start,$limit";
$JobData = all($JobSql); // 查询数据用于渲染
$ShowPage = ShowPage($page, $count, $limit); // 分页器
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>admin</title>
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- 公共样式 -->
    <?php include_once('./common/meta.php'); ?>

    <!-- jquery -->
    <script src="./assets/lib/jquery-1.7.2.min.js" type="text/javascript"></script>

    <!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="../assets/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="../assets/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="../assets/ico/apple-touch-icon-57-precomposed.png">
</head>

<!--[if lt IE 7 ]> <body class="ie ie6"> <![endif]-->
<!--[if IE 7 ]> <body class="ie ie7 "> <![endif]-->
<!--[if IE 8 ]> <body class="ie ie8 "> <![endif]-->
<!--[if IE 9 ]> <body class="ie ie9 "> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->

<body>
    <!--<![endif]-->

    <!-- 公共头部 -->
    <?php include_once('./common/header.php') ?>

    <!-- 侧边菜单栏 -->
    <?php include_once('./common/menu.php') ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">职位列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">JobList</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='JobAdd.php'"><i class="icon-plus"></i>添加职位</button>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>职位名称</th>
                                <th>所属部门</th>
                                <!-- <th>发布时间</th> -->
                                <th style="width: 30px;">操作</th>
                            </tr>
                        </thead>
                        <tbody>

                            <?php foreach ($JobData as $item) { ?>
                                <tr>
                                    <td><?php echo $item['id']; ?></td>
                                    <td><?php echo $item['name']; ?></td>
                                    <td><?php echo $item['dname']; ?></td>
                                    <td>
                                        <a href="JobEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
                                        <a href="#myModal" role="button" data-toggle="modal"><i class="icon-remove" data-id="<?php echo $item['id']; ?>"></i></a>
                                    </td>
                                </tr>
                            <?php } ?>

                        </tbody>
                    </table>
                </div>
                <div class="pagination">
                    <?php echo $ShowPage; ?>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog"
                    aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">删除</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确认删除该职位</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button class="btn btn-danger" data-dismiss="modal">删除</button>
                    </div>
                </div>
                <!-- 公共底部 -->
                <?php include_once('./common/footer.php') ?>
            </div>
        </div>
    </div>
</body>

</html>
<!-- 公共js -->
<?php include_once('./common/script.php') ?>
<script src="./config/helpers.js"></script>
<script>
    /* // 获取数据，渲染页面
    (() => {
        const limit = 10 // 用来限制显示/查询
        // 获取查询字符串部分
        const queryString = window.location.search
        // 使用 URLSearchParams 解析查询字符串
        const params = new URLSearchParams(queryString)
        const page = params.get('page') ?? 1

        $.ajax({
            type: 'post',
            url: 'api.php',
            data: {
                'action': 'select_all_job',
                limit,
                page
            },
            dataType: 'json',
            success: res => {
                console.log(res)
                if (res.code === 1) {
                    const data = res.data
                    let temp = '' // 用于拼接
                    data.forEach(item => {
                        temp += `
                        <tr>
                            <td>${item.id}</td>
                            <td>${item.name}</td>
                            <td>${item.dname}</td>
                            <td>
                                <a href="user.html"><i class="icon-pencil"></i></a>
                                <a href="#myModal" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                            </td>
                        </tr>
                        `
                    })
                    $('#data').html(temp)
                    $('.pagination').html(ShowPage())
                }
            }
        })

    })() */


    // 删除功能
    // 获取要删除的职位id
    let id = 0
    $('.icon-remove').on('click', function() {
        id = $(this).data('id')
    })

    // 发请求
    $('.btn-danger').click(() => {
        // console.log(id)
        $.ajax({
            url: 'api.php',
            type: 'post',
            data: {
                id,
                action: 'delete_job'
            },
            dataType: 'json',
            success: res => {
                if (res.code === 1) {
                    alert(res.msg)
                    location.reload()
                } else alert(res.msg)
            }
        })
    })
</script>